import 'package:boss/color_constant.dart';
import 'package:boss/main.dart';
import 'package:boss/ui/page/personalSettings/page/feedback/model/feedback_type.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/long_text_form_field.dart';
import 'package:boss/ui/widget/loading/loading.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:router/annotation.dart';

import '../../../../widget/multiple_upload_image.dart';
import '../../../../widget/radioList/row_radio.dart';

@RouterConfig(FeedbackPage.pageName)
class FeedbackPage extends StatefulWidget {
  static const pageName = '/feedbackPage';

  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  int feedbackType = FeedBackType.NOT_AVAILABLE;
  String feedbackInfo = '';
  List<String> feedbackImage = [];
  String phone;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(title: '反馈建议'),
      body: SingleChildScrollView(
        padding: EdgeInsets.symmetric(horizontal: 30.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            RowRadio<int>(
              padding: EdgeInsets.symmetric(vertical: 30.w),
              items: [
                RowRadioData<int>(title: '无法使用', value: FeedBackType.NOT_AVAILABLE),
                RowRadioData<int>(title: '功能优化建议', value: FeedBackType.OPTIMIZATION_SUGGEST),
                RowRadioData<int>(title: '其它', value: FeedBackType.OTHER),
              ],
              value: feedbackType,
              onChanged: (value) {
                setState(() {
                  feedbackType = value;
                });
              },
            ),
            LongTextFormField(
              hintText: '描述反馈信息，我们将及时优化不足处',
              maxLength: 400,
              maxLines: 8,
              onChanged: (value) {
                feedbackInfo = value;
                print(value);
              },
            ),
            MultipleUploadImage(
              images: feedbackImage,
              onChanged: (value) {
                setState(() {
                  feedbackImage = value;
                });
              },
            ),
            Container(
              margin: EdgeInsets.only(
                top: 30.w,
              ),
              child: Text(
                '联系方式：',
                style: TextStyle(
                  fontSize: 28.sp,
                  color: ColorConstant.ff33,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            BaseTextFormField(
              hintText: '请留下您在联系方式，便于了解跟进问题并服务',
              keyboardType: TextInputType.number,
              inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.integerReg)],
              textAlign: TextAlign.start,
              onChanged: (value) {
                phone = value;
              },
            ),
            Container(
              margin: EdgeInsets.only(
                top: 120.w,
              ),
              alignment: Alignment.center,
              child: RadiusButton(
                width: 590.w,
                title: '提 交',
                onTap: () async {
                  if (feedbackInfo.length < 15) {
                    toast('请输入最少15字反馈信息');
                    return;
                  }
                  try {
                    showLoadingToast();

                    var result = await api.feedbackSubmit({
                      'feedbackImage': feedbackImage.join("|"),
                      'feedbackInfo': feedbackInfo,
                      'feedbackType': feedbackType,
                      'phone': phone,
                    });
                    if (!result.successful()) {
                      toast(result.msg);
                      return;
                    }

                    toast('提交成功，我们会尽快解决您的意见');

                    Navigator.pop(context);
                  } catch (e) {
                    toast(e.toString());
                  } finally {
                    hideLoadingToast();
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
